package com.appkubes.portal.admin.application.app.menu.service.impl;

import com.appkubes.portal.admin.application.app.menu.entity.vo.MenuAdminGetVO;
import com.appkubes.portal.admin.application.app.menu.service.AppMenuService;
import com.appkubes.portal.admin.application.auth.service.KeyCloakRoleService;
import com.appkubes.portal.core.entity.PortalAppMenu;
import com.appkubes.portal.core.entity.PortalAppMenuRole;
import com.appkubes.portal.core.service.BasePortalAppMenuRoleService;
import com.appkubes.portal.core.service.BasePortalAppMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.keycloak.representations.idm.RoleRepresentation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AppMenuServiceImpl implements AppMenuService {
    @Resource
    KeyCloakRoleService roleService;

    @Resource
    BasePortalAppMenuService appMenuService; //应用菜单

    @Resource
    BasePortalAppMenuRoleService appMenuRoleService; //菜单角色


    @Override
    public HashMap<String, ArrayList<PortalAppMenu>> getAdminMenu(MenuAdminGetVO menuAdminGetVO) {
        //获取用户的角色
        List<RoleRepresentation> appAdminRoleList = roleService.getUserClientRoleList(menuAdminGetVO.getAdminKey());
        Map<String, String> appAdminRoleMap = appAdminRoleList.stream()
                .collect(Collectors.toMap(RoleRepresentation::getName, RoleRepresentation::getId));

        //获取应用的菜单
        PortalAppMenu appMenu = new PortalAppMenu();
        BeanUtils.copyProperties(menuAdminGetVO, appMenu);

        QueryWrapper<PortalAppMenu> queryMenu = new QueryWrapper();
        queryMenu.setEntity(appMenu);

        List<PortalAppMenu> appMenuList = appMenuService.getBaseMapper().selectList(queryMenu);

        //确定用户角色是否对菜单有访问权限
        PortalAppMenuRole queryAppMenuRole = new PortalAppMenuRole();

        QueryWrapper<PortalAppMenuRole> queryMenuRole = new QueryWrapper();
        queryMenuRole.setEntity(queryAppMenuRole);

        List<PortalAppMenuRole> appMenuRoleList = appMenuRoleService.getBaseMapper().selectList(queryMenuRole);

        //过滤用户角色对应的有效菜单
        appMenuRoleList = appMenuRoleList.stream().filter(appMenuRoleElement -> {
            if ("ACTIVE".equalsIgnoreCase(appMenuRoleElement.getStatus())) {
                //有效
                String roleCode = appMenuRoleElement.getRoleCode();
                if (appAdminRoleMap.containsKey(roleCode)) {
                    return true;
                }
            }
            return false;
        }).collect(Collectors.toList());
        Map<String, String> appMenuRoleMap = appMenuRoleList.stream().collect(
                Collectors.toMap(PortalAppMenuRole::getMenuKey, PortalAppMenuRole::getRoleCode)
        );


        HashMap<String, ArrayList<PortalAppMenu>> menuMap = new HashMap<>(); //允许NULL为Key的Map
        //从菜单中过程有权限的菜单
        appMenuList.stream().filter(appMenuElement -> {
            String menuKey = appMenuElement.getMenuKey();
            return appMenuRoleMap.containsKey(menuKey);
        }).map(appMenuElement -> {
            //处理为返回格式
            String parentMenuKey = appMenuElement.getParentMenuKey();
            ArrayList<PortalAppMenu> menus = menuMap.get(parentMenuKey);
            if (menus == null) {
                menus = new ArrayList<>();
            }
            menus.add(appMenuElement);
            menuMap.put(parentMenuKey, menus);
            return 0;
        });


        return menuMap;
    }
}
